#include "Game.h"

#define HEIGHTSIZE 8.3f
#define WIDTHSIZE 11.07f
#define HEIGHTSIZE_2 2.0f
#define WIDTHSIZE_2 2.750f
#define CARTMANSPEED 0.06f

IGame* createGame( HINSTANCE hInst, HWND hWnd )
{
	return new Game( hInst, hWnd );
}

Game::Game( HINSTANCE hInst, HWND hWnd ) :
	m_input( hInst, hWnd )
{
	// initialize camera
	m_camera.hand = Camera::RIGHTHANDED;

	m_camera.eye = float3( 0, 0, 20.f );
	m_camera.at  = float3( 0, 0, 0 );
	m_camera.up  = float3( 0, 1, 0 );

	m_camera.projectionType = Camera::PERSPECTIVE_FOV;
	m_camera.perspectiveFov.fovy = D3DXToRadian(45.f);
	m_camera.perspectiveFov.aspect = 800.f/600.f;

	m_camera.zn = 1.0f;
	m_camera.zf = 100.0f;

	x = float3(0,-6,0);
}

Game::~Game()
{
	DestroyVertexDeclarations();
	m_d3dDevice = 0;
}

void Game::onCreateGraphics( D3DDevice device )
{
	m_d3dDevice = device;
	
	renderer1.Initialize(m_d3dDevice);

	speed = 2;

	b.Initialize( m_d3dDevice );
	a.Initialize( m_d3dDevice );

	// initialize polygon b
	b.addVertex(float3( -WIDTHSIZE, -HEIGHTSIZE, 0 ), float2(0,1) );
	b.addVertex(float3( WIDTHSIZE, -HEIGHTSIZE, 0 ) , float2(1,1) );
	b.addVertex(float3( WIDTHSIZE, HEIGHTSIZE, 0 )  , float2(1,0) );
	b.addVertex(float3( -WIDTHSIZE, HEIGHTSIZE, 0 ) , float2(0,0) );

	a.addVertex(float3( -WIDTHSIZE_2, -HEIGHTSIZE_2, 0 ), float2(0,1) );
	a.addVertex(float3( WIDTHSIZE_2, -HEIGHTSIZE_2, 0 ) , float2(1,1) );
	a.addVertex(float3( WIDTHSIZE_2, HEIGHTSIZE_2, 0 )  , float2(1,0) );
	a.addVertex(float3( -WIDTHSIZE_2, HEIGHTSIZE_2, 0 ) , float2(0,0) );

	Node1.AttachChild(&b);
	Node1.AttachChild(&a);
	Scene.AttachChild(&Node1);

	
	b.applyTexture("grassback2.bmp");
	b.setup();

	
	a.applyTexture("cartman2.bmp");
	a.setup();

	RS_AlphaBlend defaultAlpha;
	RS wireframe;
	a.SetRenderState(defaultAlpha);

	a.Local = translation(x);
	Scene.UpdateGS(1.0f);

	//Initializes all vertex declarations
	InitVertexDeclarations(m_d3dDevice);

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 world = identity44();
	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_WORLD, &world );
	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );
}

void Game::onResetGraphics( D3DDevice device )
{
	//TODO: Add code to handle lost device ( resizing issue )

	m_d3dDevice = device;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );
}

void Game::onLostGraphics()
{
}

void Game::onFrameMove(float dt)
{
	a.Local = translation(x);
	a.UpdateGS(dt);

	if( GetAsyncKeyState( VK_LEFT ) & 0x8000 )
	{
		x -= float3(CARTMANSPEED, 0, 0);
	}

	if( GetAsyncKeyState( VK_RIGHT ) & 0x8000 )
	{
		x += float3(CARTMANSPEED, 0, 0);
	}
}

void Game::onFrameRender(float dt)
{
	float44 world = identity44();

	//Only one call to draw entire scene is required
	renderer1.DrawScene(&Scene);
}

void Game::onDestroyGraphics()
{
}
